package org.bw.bookstore.book.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.bw.bookstore.book.mapper.BookDetailedMapper;
import org.bw.bookstore.book.mapper.InventoryRecordMapper;
import org.bw.bookstore.book.mapper.InventoryTaskMapper;
import org.bw.bookstore.book.mapper.StaffMemberMapper;
import org.bw.bookstore.book.service.InventoryRecordService;
import org.bw.bookstore.common.entity.TbBookDetailed;
import org.bw.bookstore.common.entity.TbInventoryRecord;
import org.bw.bookstore.common.entity.TbInventoryTask;
import org.bw.bookstore.common.entity.TbStaffMember;
import org.bw.bookstore.common.utils.ResponseResult;
import org.bw.bookstore.common.utils.StringTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
*@program: wisdom_bookstore
*@description: 盘点记录接口实现
*@author: 孙冬雪
*@create: 2020-10-22 10:48
*/
@Service
public class InventoryRecordServiceImpl implements InventoryRecordService {

    @Autowired
    private InventoryRecordMapper inventoryRecordMapper;
    @Autowired
    private StaffMemberMapper staffMemberMapper;
    @Autowired
    private InventoryTaskMapper inventoryTaskMapper;
    @Autowired
    private BookDetailedMapper bookDetailedMapper;

    @Override
    public ResponseResult makeInventoryRecord(TbInventoryRecord inventoryRecord) {
        ResponseResult result = ResponseResult.SUCCESS();

        //判断工作人员是否存在
        TbStaffMember tbStaffMember = staffMemberMapper.selectOne(new QueryWrapper<TbStaffMember>()
                .lambda().eq(TbStaffMember::getCode,inventoryRecord.getStaffMemberCode()));
        if(tbStaffMember == null || !tbStaffMember.getDeleted().equals(0)) {
            return result.setFail("工作人员不存或已离职！");
        }

        //判断盘点任务是否存在
        TbInventoryTask tbInventoryTask = inventoryTaskMapper.selectOne(new QueryWrapper<TbInventoryTask>()
                .lambda().eq(TbInventoryTask::getCode,inventoryRecord.getInventoryTaskCode()));
        if(tbInventoryTask == null || !tbInventoryTask.getDeleted().equals(0)) {
            return result.setFail("盘点任务不存在！");
        }

        //查询图书详情  判断是否有该类书
        TbBookDetailed tbBookDetailed = bookDetailedMapper.selectOne(new QueryWrapper<TbBookDetailed>()
                .lambda().eq(TbBookDetailed::getCode,tbInventoryTask.getBookDetailedCode()));
        if(tbBookDetailed == null || !tbBookDetailed.getDeleted().equals(0)){
            return result.setFail("图书不存在，请先添加新品！或已下架!");
        }

        TbInventoryRecord tbInventoryRecord = inventoryRecordMapper.selectOne(new QueryWrapper<TbInventoryRecord>()
        .lambda().eq(TbInventoryRecord::getInventoryTaskCode,inventoryRecord.getInventoryTaskCode()));

        if(tbInventoryRecord!=null){
            tbInventoryRecord.setInventoryNum(tbInventoryRecord.getInventoryNum()+inventoryRecord.getInventoryNum());
            inventoryRecordMapper.updateById(tbInventoryRecord);

            TbInventoryRecord tbInventoryRecord1 = inventoryRecordMapper.selectOne(new QueryWrapper<TbInventoryRecord>()
                    .lambda().eq(TbInventoryRecord::getId,tbInventoryRecord.getId()));

            tbInventoryRecord1.setExceedLack(tbInventoryRecord1.getInventoryNum()-tbBookDetailed.getBookInventoryLevel());
            tbInventoryRecord1.setUpdateTime(new Date());
            inventoryRecordMapper.updateById(tbInventoryRecord1);

            if(tbInventoryRecord1.getInventoryNum()==tbBookDetailed.getBookInventoryLevel()){
                tbInventoryTask.setFinishStatus(1);
                tbInventoryTask.setInventoryTime(new Date());
                inventoryTaskMapper.updateById(tbInventoryTask);
            }
            return result;
        }else {
            //添加数据
            inventoryRecord.setCreateTime(new Date());
            inventoryRecord.setCode(StringTools.getRandomString(32));
            inventoryRecord.setDeleted(0);
            inventoryRecord.setFinishTime(new Date());
            inventoryRecord.setInventoryTaskCode(inventoryRecord.getInventoryTaskCode());
            inventoryRecord.setStaffMemberCode(inventoryRecord.getStaffMemberCode());
            inventoryRecord.setInventoryNum(inventoryRecord.getInventoryNum());
            inventoryRecord.setExceedLack(inventoryRecord.getInventoryNum()-tbBookDetailed.getBookInventoryLevel());
            inventoryRecordMapper.insert(inventoryRecord);

            if(inventoryRecord.getId()!=null){
                return result;
            }
            return result.setFail("生成盘点记录失败");
        }
    }


    @Override
    public ResponseResult findInventoryRecordList() {
        ResponseResult result = ResponseResult.SUCCESS();
        HashMap<String, Object> resultMap = new HashMap<>();

        //查询全部记录
        List<TbInventoryRecord> inventoryRecordList=inventoryRecordMapper.findInventoryRecordList();

        resultMap.put("inventoryRecordList",inventoryRecordList);
        result.setResult(resultMap);
        return result;
    }
}
